package com.yunfangdata.service.impl;

import com.yunfangdata.domain.Employee;
import com.yunfangdata.domain.Salary;
import com.yunfangdata.mapper.SalaryMapper;
import com.yunfangdata.query.QueryObject;
import com.yunfangdata.service.ISalaryService;
import com.yunfangdata.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SalaryServiceImpl implements ISalaryService {


    @Autowired
    private SalaryMapper mapper;

    public void save(Salary entity) {
        //1.只保存了日期
        //2.保存编辑人,从session中找到人

         Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
         entity.setInputUser(currentUser);


        entity.setInputTime(new Date());
        System.out.println(entity.getMonth());;
        mapper.insert(entity);
    }

    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void update(Salary entity) {
        mapper.updateByPrimaryKey(entity);
    }

    public Salary get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Salary> list() {
        return mapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int total = mapper.queryForCount(qo);

        if (total == 0){
            return new PageResult();
        }
        List<Salary> rows = mapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    public void changeState(Long id) {
        Salary salary = mapper.selectByPrimaryKey(id);
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        salary.setApprover(currentUser);
        salary.setStatus(1);
        mapper.updateByPrimaryKey(salary);


    }
}
